import QtQuick 2.15
import QtQuick.Controls 2.15
import '../../controls'

Rectangle {
    anchors.fill: parent
    gradient: mainGradient
    property var queryData: null

    Component.onCompleted: {
        window.title=qsTr('乐园管理系统')
        timerQuery.start()
    }

    function queryDuiHuan()
    {
        //最新兑换记录刷新
        var request = HttpHelper.createIMRequest();
        request.onSucceed( function(request){
            // console.log('------water_get_exchange_record-------',JSON.stringify(request.responseData))
            lvRight.model=request.responseData.list
        } );
        request.onError(  function(request){
            //CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_get_exchange_record?page=1&limit=20")
        queryDayPrice()
    }
    function queryDayPrice()
    {
        //当日交易金额
        var request = HttpHelper.createIMRequest();
        request.onSucceed( function(request){
            queryData=request.responseData
        } );
        request.onError(  function(request){
            // CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_get_cost_total")
    }

    function loadCostRecord()
    {
        var request = HttpHelper.createIMRequest();
        //        request.enabledLog=true
        request.onSucceed( function(request){
            queryDuiHuan()
            // console.log('--------water_get_cost_record---',JSON.stringify(request.responseData))
            lvs.model = request.responseData.list
        } );
        request.onError(  function(request){
            console.log('--------water_get_cost_record--error-',request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_get_cost_record?page=1&limit=20")
    }
    Connections{
        target: window
        function onRefreshData()
        {
            loadCostRecord()
        }
    }
    Timer{

        id:timerQuery
        interval: 10000
        repeat: true
        triggeredOnStart : true
        onTriggered: {
            loadCostRecord()

        }
    }

    Item {
        id:itemTitle
        width: parent.width
        height: 60
        Text {
            color: '#ffffff'
            anchors.verticalCenter: parent.verticalCenter
            x:20
            text: qsTr("最新游玩情况")
        }
        Row{
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: parent.right
            anchors.rightMargin: 20
            spacing: 20
            Text {
                color: '#ffffff'
                anchors.verticalCenter: parent.verticalCenter
                text: {
                    if(queryData && 'sale_total' in queryData)
                    {
                        return  qsTr("今日总金额: ￥")+queryData.sale_total.toFixed(2)+qsTr('元')
                    }
                    return ''
                }
            }
            Text {
                color: '#ffffff'
                anchors.verticalCenter: parent.verticalCenter
                text: {
                    if(queryData && 'times' in queryData)
                    {
                        return  qsTr("今日游玩总量: ")+queryData.times+qsTr('次')
                    }
                    return ''
                }
            }
        }
    }
    Item {
        anchors.fill: parent
        anchors.topMargin: itemTitle.height +10
        anchors.bottomMargin: itemBottom.height + 10
        anchors.leftMargin: 20
        anchors.rightMargin: 20
        Item {
            id:itemRight
            width: 320
            height: parent.height
            anchors.right: parent.right
            Rectangle{
                anchors.fill: parent
                color: 'black'
                opacity: 0.4
            }
            Item {
                width: parent.width
                height: 60
                Text {
                    font.pixelSize: 20
                    color: '#ffffff'
                    font.bold: true
                    anchors.centerIn: parent
                    text: qsTr("最新兑换")
                }
            }
            ListView{
                id:lvRight
                width: parent.width
                height: parent.height-y
                model: []
                y:60
                clip: true
                ScrollBar.vertical: ScrollBar{}
                delegate: Item{
                    width: lvRight.width
                    height: 60
                    Rectangle{
                        anchors.fill: parent
                        color:  'black'
                        opacity: index % 2 ===0 ?0.4:0
                    }
                    Column{
                        anchors.verticalCenter: parent.verticalCenter
                        x:10
                        spacing: 4
                        property var item: lvRight.model[index]
                        Text {
                            color: '#ffffff'
                            font.pixelSize: 14
                            text: qsTr("设备编号: ")+ parent.item.b_name
                        }
                        Text {
                            color: '#999999'
                            font.pixelSize: 10
                            text: qsTr("兑换时间: ")+parent.item.create_time
                        }
                    }

                }
            }
        }
        Rectangle {
            border.color: '#dddddd'
            width: parent.width - itemRight.width - 10
            height: parent.height
            Rectangle {
                id:itemHeader
                width: parent.width - 2
                anchors.horizontalCenter: parent.horizontalCenter
                height: 40
                color: '#F5F7FA'
                property var itemWidth: width / repHeader.model.length
                Row{
                    anchors.verticalCenter: parent.verticalCenter
                    height: parent.height
                    width: parent.width
                    Repeater{
                        id:repHeader
                        model: [qsTr('编号'),qsTr('用户头像'),qsTr('用户昵称'),qsTr('性别'),qsTr('订单编号'),qsTr('手环号'),qsTr('消费金额'),qsTr('兑换时间'),qsTr('归还时间'),qsTr('状态')]
                        Text {
                            width: itemHeader.itemWidth
                            height: itemHeader.height
                            color: '#E0000000'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: modelData
                            elide: Text.ElideRight
                            anchors.verticalCenter: parent.verticalCenter
                        }
                    }
                }
            }
            ListView{
                id:lvs
                anchors.fill: parent
                anchors.topMargin: itemHeader.height
                model: []
                ScrollBar.vertical: ScrollBar{}
                clip: true
                property var itemWidth: width / repHeader.model.length
                property var itemHeight: 40
                boundsBehavior: Flickable.StopAtBounds
                delegate: Rectangle{
                    width: lvs.width
                    height: lvs.itemHeight
                    color: index % 2 === 0 ? '#FFF2F2F2' : '#ffffff'
                    property var itemData: lvs.model[index]
                    Row{
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.id
                            elide: Text.ElideRight
                        }
                        Item {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            AvatarItem{
                                width: lvs.itemHeight - 10
                                height: lvs.itemHeight - 10
                                source:
                                {
                                    if('avatar' in itemData && itemData.avatar)
                                    {
                                        return itemData.avatar
                                    }
                                    return''

                                }
                                anchors.centerIn: parent
                            }
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.w_nickname
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.sex === 0 ? '男' : '女'
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text:itemData.w_no
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.b_name
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.total.toFixed(2)
                            elide: Text.ElideRight
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.create_time
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: itemData.back_time
                            elide: Text.ElideRight
                            wrapMode: Text.WrapAnywhere
                        }
                        Text {
                            width: lvs.itemWidth
                            height: lvs.itemHeight
                            color: '#333333'
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            text: {
                                switch(itemData.p_status)
                                {
                                case 0:
                                    return qsTr('待支付')
                                case 1:
                                    return qsTr('已支付')
                                case 2:
                                    return qsTr('已退款')
                                case 3:
                                    return qsTr('已取消')
                                case 10:
                                    return qsTr('未消费')
                                }
                                return  qsTr('未知类型:')+itemData.p_status
                            }
                            elide: Text.ElideRight
                        }

                    }

                }
            }
        }

    }

    Item {
        id: itemBottom
        width: parent.width
        anchors.bottom: parent.bottom
        height: 100
        Row{
            anchors.centerIn: parent
            spacing: 40
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#ffffff'
                    Text {
                        anchors.centerIn: parent
                        color: '#333333'
                        text: qsTr("统计查询")
                    }
                }
                onClicked: {
                    window.addPage('qrc:/main/mamager/StatisticsQueryView.qml')
                }
            }
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#EA9508'
                    Text {
                        anchors.centerIn: parent
                        color: '#ffffff'
                        text: qsTr("手环兑换")
                    }
                }
                onClicked: {
                    window.addPage('qrc:/main/mamager/settlement/PayDepositView.qml')
                }
            }
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#319B0B'
                    Text {
                        anchors.centerIn: parent
                        color: '#ffffff'
                        text: qsTr("手环查询")
                    }
                }
                onClicked: {
                    CGlobal.createObjectAndOpen(window,'qrc:/main/InputBraceletNoDialog.qml',
                                                {
                                                    qmlPath:'qrc:/main/mamager/settlement/BraceletInfoView.qml',
                                                    properties:{}
                                                })
                    //window.addPage('qrc:/main/mamager/settlement/BraceletInfoView.qml')
                }
            }
        }
    }

}
